<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>System Tools</title>
	<{include file="system/header.tpl"}>
	<script typpe="text/javascript" src="<{'static/js/select2/select2.min.js'|url}>"></script>
	<link rel="stylesheet" href="<{'static/js/select2/select2.css'|url}>" />
	<link rel="stylesheet" href="<{'static/js/select2/select2-bootstrap.css'|url}>" />
	<style>
	
	</style>
</head>
<body>
<{include file="system/nav.tpl"}>

<div class="container" role="main">
	<div class="page-header">
		<h1>脚手架<div class="pull-right"><button type="button" class="btn btn-info">预览</button></div></h1>
		<div class="clearfix"></div>
	</div>
	
	<form role="form" action="<{'wizard/'}>" class="form-horizontal" method="POST">
	<{foreach $_column_list as $table_name => $item}>
		<div class="row" name="table[<{$table_name|escape}>]" data-table_name="<{$table_name|escape}>">
			<h3 class="page-header"><{$table_name|@escape}></h3>
			<div class="col-md-5">
				<table class="table table-bordered table-hover table-condensed table-striped" >
					<thead>
						<tr class="info">
							<th colspan="5" class="text-center"><{$table_name|@escape}></th>
						</tr>
						<tr>
							<td>字段名称</td>
							<td>类型</td>
							<td>注释</td>
							<td>搜索</td>
							<td>表单</td>
						</tr>
					</thead>
					<tbody>
						<{foreach $item.columns as $key => $field}>
						<tr <{foreach $field as $k => $v}>data-<{$k|escape}>="<{$v|escape}>"<{/foreach}>>
							<td><{$key|escape}><{if $field.key == 'PRI'}> <i class="glyphicon glyphicon-pushpin" title="主键"></i><{/if}></td>
							<td><{$field.data_type}>(<{$field.display|escape}><{$field.character_maximum_length}>)</td>
							<td><{$field.comment|escape}></td>
							<td>
								<select name="search_type">
									<option value="">不搜索</option>
									<option value="in">IN</option>
									<option value="equal">=</option>
									<option value="like">LIKE</option>
									<option value="greater">&gt;</option>
									<option value="greater_equal">&gt;=</option>
									<option value="less">&lt;</option>
									<option value="less_equal">&lt;=</option>
									<option value="range">RANGE</option>
								</select>
							</td>
							<td><input type="checkbox" name="form_type" value="" /></td>
						</tr>
						<{/foreach}>
					</tbody>
				</table>
			</div>
			<div class="col-md-7">
				<div class="form-group">
					<label for="method_postfix<{$item@index}>" class="control-label">方法后缀：</label>
					<input type="text" name="method_postfix" id="method_postfix<{$item@index}>" data-table_name="<{$table_name|escape}>" class="form-control" value="" placeholder="下划线开头，或者不输入">
				</div>
				<ul class="nav nav-tabs" role="tablist">
					<li class="active"><a href="#search<{$item@index}>" role="tab" data-toggle="tab">搜索</a></li>
					<li><a href="#form<{$item@index}>" role="tab" data-toggle="tab">表单</a></li>
				</ul>
				<div class="tab-content">
  					<div class="tab-pane active" id="search<{$item@index}>">
  						<label for="code<{$item@index}>" class="control-label">代码预览：</label>
						<pre class="control-label-static pre-scrollable"><code style="word-wrap:nowrap;">public function search<mark name="postfix"></mark>($fields, $order_by = array('a.timeline' => 'DESC'), $page = 1, $pagesize = 0)<br />{<br />	$_fields = array(<span name="fields"></span>);<br />	$fields = to_array_selector($fields, '<span name="to_array"></span>');<br />	$fields = _extends($fields, $_fields);<br /><br />	$query = DB::select()->from(array('<{$table_name|escape}>','a'));<br /><span name="search"></span><br />	foreach ($order_by as $key => $value)<br />		$query->order_by($key, $value);<br /><br />	$result = $this->make_page($query, 'a.*', 'a.<{$item.primary_key|escape}>', '<{$item.primary_key|escape}>', $page, $pagesize);<br />	return $result;<br />}</code></pre>
  					</div>
  					<div class="tab-pane" id="form<{$item@index}>">
  						<label for="code<{$item@index}>" class="control-label">代码预览：</label>
						<pre class="control-label-static pre-scrollable"><code style="word-wrap:nowrap;"></code></pre>
  					</div>
				</div>
			</div>
			<div class="clearfix"></div>
		</div>
		<{/foreach}>
		<div class="form-group">
			
		</div>
	</form>
	
	<p></p>
	<p></p>
</div>
<{include file="system/footer.tpl"}>
<script type="text/javascript">
(function($){
	$().ready(function(){
		$('li.nav-wizard','#navigation').addClass('active');
		$('a[method],form').query();
		

		var method = {};
		method.make_search_code = function(table_name){
			var $table = $('[name="table['+table_name+']"]');
			var $select = $('select[name="search_type"]', $table);
			
			var fields = [], search = [], to_array = [];
			for(var i = 0; i < $select.length; ++i){
				var $s = $select.eq(i);
				var val = $s.val();
				var column_name = $s.closest('tr').data('column_name');
				
				switch(val){
					case 'in':
						fields.push('\''+ column_name +'\' => array()');
						to_array.push(column_name);
						search.push("\t" + '!empty($fields[\''+ column_name +'\']) && $query->and_where(\'a.'+column_name+'\',\'IN\',$fields[\''+ column_name +'\']);');
					break;
					case 'equal':
						fields.push('\''+ column_name +'\' => NULL');
						search.push("\t" + '!empty($fields[\''+ column_name +'\']) && $query->and_where(\'a.'+column_name+'\',\'=\',$fields[\''+ column_name +'\']);');
					break;
					case 'less':
						fields.push('\''+ column_name +'\' => NULL');
						search.push("\t" + '!empty($fields[\''+ column_name +'\']) && $query->and_where(\'a.'+column_name+'\',\'<\',$fields[\''+ column_name +'\']);');
					break;
					case 'less_equal':
						fields.push('\''+ column_name +'\' => NULL');
						search.push("\t" + '!empty($fields[\''+ column_name +'\']) && $query->and_where(\'a.'+column_name+'\',\'<=\',$fields[\''+ column_name +'\']);');
					break;
					case 'greater':
						fields.push('\''+ column_name +'\' => NULL');
						search.push("\t" + '!empty($fields[\''+ column_name +'\']) && $query->and_where(\'a.'+column_name+'\',\'>\',$fields[\''+ column_name +'\']);');
					break;
					case 'greater_equal':
						fields.push('\''+ column_name +'\' => NULL');
						search.push("\t" + '!empty($fields[\''+ column_name +'\']) && $query->and_where(\'a.'+column_name+'\',\'>=\',$fields[\''+ column_name +'\']);');
					break;
					case 'like':
						fields.push('\''+ column_name +'\' => \'\'');
						search.push("\t" + '!empty($fields[\''+ column_name +'\']) && $query->and_where(\'a.'+column_name+'\',\'LIKE\',\'%\'.$fields[\''+ column_name +'\'].\'%\');');
					break;
					case 'range':
						fields.push('\''+ column_name +'\' => array(\'min\' => NULL, \'max\' => NULL, )');
						search.push("\t" + '!empty($fields[\''+ column_name +'\'][\'min\']) && $query->and_where(\'a.'+column_name+'\',\'>=\',$fields[\''+ column_name +'\'][\'min\']);');
						search.push("\t" + '!empty($fields[\''+ column_name +'\'][\'max\']) && $query->and_where(\'a.'+column_name+'\',\'<=\',$fields[\''+ column_name +'\'][\'max\']'+ (column_name.indexOf('timeline')>=0 ? ' + 86400' : '') +');');
					break;
				}
			}
			$('[name="fields"]', $table).text(fields.join(', '));
			$('[name="search"]', $table).text(search.join("\n"));
			$('[name="to_array"]', $table).text(to_array.join(','));
		}

		method.make_form_code = function(table_name)
		{

		}

		$('select[name="search_type"]').each(function(){
			var $this = $(this);
			var $tr = $this.closest('tr');
			var data_type = $tr.data('data_type');
			var type = $tr.data('type');
			var column_name = $tr.data('column_name');

			if (data_type == 'text'){}
			else if (type == 'string')
				$this.val('like');
			else if (column_name.indexOf('timeline') >= 0)
				$this.val('range');
			else
				$this.val('in');
		}).on('change',function(){
			var $table = $(this).closest('[name^="table["]');
			method.make_search_code( $table.data('table_name'));
		});

		$('input[name="form_type"]').each(function(){
 			var $this = $(this);
			var $tr = $this.closest('tr');
			var extra = $tr.data('extra');
			var column_name = $tr.data('column_name');

			if (extra != 'auto_increment' && ['timeline','uid'].indexOf(column_name) < 0)
				$this.prop('checked', true);
		});
		$('input[name="method_postfix"]').on('input change',function(){
			var $table = $(this).closest('[name^="table["]');
			$('[name="postfix"]', $table).text(this.value);
		});

		$('[name^="table["').each(function(){
			var $this = $(this);
			var table_name = $this.data('table_name');
			$('input[name="method_postfix"]', $this).val((index = table_name.indexOf('_')) >= 0 ? table_name.substr(index) : '').triggerHandler('change');
			method.make_search_code(table_name);
			method.make_form_code(table_name);
		})
		
	});
})(jQuery);
</script>
</body>
</html>